package MongoDAL;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import DALFactory.IDataCommentMapper;
import Entity.ERankElement;

import com.mongodb.*;

/**
 * 评论表（Comment）数据操作
 * 
 * @author Administrator
 */
public class MongoDataCommentMapper implements IDataCommentMapper {
	/*
	 * (non-Javadoc)
	 * 
	 * @see MongoDAL.IDataCommentMapper#checkComment(int, int)
	 */

	@Override
	public boolean checkComment(int HID, int UID) {
		// 构建连接
		MongoStatement mstm = new MongoStatement();
		// 选择评分集合
		DBCollection CommentColl = mstm.QueryTable("Comment");
		// 选择房屋信息集合
		DBCollection HouseColl = mstm.QueryTable("House");
		// 选择用户信息集合
		DBCollection UserColl = mstm.QueryTable("User");
		// 以用户ID（UID）查找用户对象
		DBObject user = new BasicDBObject();
		user.put("UID", UID);
		DBObject userValue = UserColl.findOne(user);

		// 以房屋ID（_id）查找房屋对象
		DBObject house = new BasicDBObject();
		house.put("_id", HID);
		DBObject houseValue = HouseColl.findOne(house);
		// 构建数据库引用对象（房屋对象）
		DBRef HouseDbf = new DBRef(HouseColl.getDB(), "House",
				houseValue.get("_id"));
		// 构建数据库引用对象（用户对象）
		DBRef UserDbf = new DBRef(UserColl.getDB(), "User",
				userValue.get("UID"));

		// 构建房屋查找目标对象
		DBObject targer = new BasicDBObject();
		DBObject UserElement = new BasicDBObject();
		DBObject UIDObject = new BasicDBObject();
		
		UIDObject.put("UID", UserDbf);
		// **关键部分，使用$elemMatch命令来匹配查询
		UserElement.put("$elemMatch", UIDObject);
	
		targer.put("User", UserElement);
		targer.put("HID", HouseDbf);
	
		
		// 假如对象不存在则返回false

		if (CommentColl.findOne(targer) == null) {
			mstm=null;
			return false;
		} else {
			mstm=null;
			return true;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see MongoDAL.IDataCommentMapper#getStar(int)
	 */
	@Override
	public int[] getStar(int HID) {
		MongoStatement mstm = new MongoStatement();

		DBCollection HouseColl = mstm.QueryTable("House");
		DBCollection CommentColl = mstm.QueryTable("Comment");

		DBObject house = new BasicDBObject();
		house.put("_id", HID);
		DBObject HouseValue = HouseColl.findOne(house);
		DBRef HouseDbf = new DBRef(HouseColl.getDB(), "House",
				HouseValue.get("_id"));

		DBObject targer = new BasicDBObject();

		targer.put("HID", HouseDbf);

		DBObject result = CommentColl.findOne(targer);

		int[] Stars = new int[5];
		Stars[0] = Integer.parseInt(result.get("OneStar").toString());
		Stars[1] = Integer.parseInt(result.get("TwoStar").toString());
		Stars[2] = Integer.parseInt(result.get("ThreeStar").toString());
		Stars[3] = Integer.parseInt(result.get("FourStar").toString());
		Stars[4] = Integer.parseInt(result.get("FiveStar").toString());
		mstm=null;
		return Stars;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see MongoDAL.IDataCommentMapper#InsertComment(int, int, int)
	 */
	@Override
	public boolean InsertComment(int HID, int UID, int Mark) {

		// 新建MongoDB语句
		MongoStatement mstm = new MongoStatement();
		// 参照完整性
		DBCollection HouseColl = mstm.QueryTable("House");
		DBObject house = new BasicDBObject();
		house.put("_id", HID);
		DBObject HouseValue = HouseColl.findOne(house);

		DBCollection UserColl = mstm.QueryTable("User");
		DBObject user = new BasicDBObject();
		user.put("UID", UID);
		DBObject userValue = UserColl.findOne(user);

		if (HouseValue == null || userValue == null) {
			return false;
		} else {
			// 设置参照 HID、UID
			DBRef HouseDbf = new DBRef(HouseColl.getDB(), "House",
					HouseValue.get("_id"));
			DBRef UserDbf = new DBRef(UserColl.getDB(), "User",
					userValue.get("UID"));
			// 更新或插入对象
			DBCollection CommentColl = mstm.QueryTable("Comment");

			DBObject insertValue = new BasicDBObject();
			insertValue.put("HID", HouseDbf);
			DBObject obj = CommentColl.findOne(insertValue);

			DBObject newUser = new BasicDBObject();
			newUser.put("UID", UserDbf);
			newUser.put("Mark", Mark);

			if (obj != null) {// 更新对象

				if (checkComment(HID, UID)) {
					mstm=null;
					return false;
				}
				DBObject temp = new BasicDBObject();
				temp.put("User", newUser);
				DBObject incValue = new BasicDBObject();

				switch (Mark) {
				case 1:
					incValue.put("OneStar", 1);
					break;
				case 2:
					incValue.put("TwoStar", 1);
					break;
				case 3:
					incValue.put("ThreeStar", 1);
					break;
				case 4:
					incValue.put("FourStar", 1);
					break;
				case 5:
					incValue.put("FiveStar", 1);
					break;
				}

				DBObject updateValue = new BasicDBObject();
				updateValue.put("$push", temp);
				updateValue.put("$inc", incValue);
				CommentColl.update(insertValue, updateValue);
			} else {// 插入对象
				DBObject[] newUsers = { newUser };
				insertValue.put("User", newUsers);

				int OneStar = 0;
				int TwoStar = 0;
				int ThreeStar = 0;
				int FourStar = 0;
				int FiveStar = 0;

				switch (Mark) {
				case 1:
					OneStar++;
					break;
				case 2:
					TwoStar++;
					break;
				case 3:
					ThreeStar++;
					break;
				case 4:
					FourStar++;
					break;
				case 5:
					FiveStar++;
					break;
				}
				insertValue.put("OneStar", OneStar);
				insertValue.put("TwoStar", TwoStar);
				insertValue.put("ThreeStar", ThreeStar);
				insertValue.put("FourStar", FourStar);
				insertValue.put("FiveStar", FiveStar);
				CommentColl.insert(insertValue);
			}
			mstm=null;
			return true;
		}
	}

	
	@Override
	public List<ERankElement> getStarRank() {

		MongoStatement mstm = new MongoStatement();
		// 选择评分集合
		DBCollection CommentColl = mstm.QueryTable("Comment");
		List<ERankElement> result= new ArrayList<ERankElement>();

		DBCursor cur = CommentColl.find();
		while (cur.hasNext()) {
			ERankElement element=new ERankElement();
			DBObject temp= cur.next();
			
			DBRef HouseDbf =(DBRef) temp.get("HID") ;
			element.setHID(Integer.parseInt(HouseDbf.getId().toString()));
			int OneStar=Integer.parseInt(temp.get("OneStar").toString());
			int TwoStar=Integer.parseInt(temp.get("TwoStar").toString());
			int ThreeStar=Integer.parseInt(temp.get("ThreeStar").toString());
			int FourStar=Integer.parseInt(temp.get("FourStar").toString());
			int FiveStar=Integer.parseInt(temp.get("FiveStar").toString());
			int mark=1*OneStar+2*TwoStar+3*ThreeStar+4*FourStar+5*FiveStar;
			element.setMark(mark);
			result.add(element);
		}
		Collections.sort(result);
		mstm=null;
		return result;
	}
	public static void main(String[] args) {
		IDataCommentMapper mdm5 = new MongoDataCommentMapper();
		boolean b = mdm5.InsertComment(904, 10003,5);
		System.out.println(b);
		System.out.println(mdm5.getStar(363)[0]);
		System.out.println(mdm5.getStar(363)[1]);
		System.out.println(mdm5.getStar(363)[2]);
		System.out.println(mdm5.getStar(363)[3]);
		System.out.println(mdm5.getStar(363)[4]);
	}
}
